var page = 1;
var pagesize = 20;
var type = 1;//默认tab值为：‘数据库’

var target_div_box = "db_table_content";
var tempalte_div_box = "db_list_template";

$(function (){
	//tab切换
	$('._tab_li').click(function (){
		var obj = $(this);
		type = obj.attr('data-type');
		if(type==1){
			target_div_box = "db_table_content";
			tempalte_div_box = "db_list_template";
		}else{
			target_div_box = "txt_table_content";
			tempalte_div_box = "txt_list_template";
		}

		getlist({page:1,type:type,pagesize:pagesize});
	});

	//初始化列表
	getlist();

	//注册日期格式化方法
	template.helper('dateFormat', dateFormat);

	//删除动作
	// $('.bs-example-modal-sm-close').on('show.bs.modal', function () {
	//   // 执行一些动作...
	//   alert('aaa');
	//   	var obj = $(this);
	// 	var id = obj.attr('data-id');
	// 	var name = obj.attr('data-name');
	// 	alert(id+"==="+name);
	// })
	$(document)
		.on('click','._view_delete',function (){
			var obj = $(this);
			var id = obj.attr('data-id');
			var name = obj.attr('data-name');
			var type = obj.attr('data-type');

			$('._delete_text').html('确定删除'+name+"?");
			$('._delete_id').val(id);
			$('._delete_type').val(type);
		})
		.on('click','._delete_submit',function (){
			//确定删除
			var data = {};
			data.id = $('._delete_id').val();
			data.type = $('._delete_type').val();
			
			$.ajax({
			    url:'/Structure/ajaxDelete.html',
			    type:'POST',
			    dataType:'json',
			    async:true,
			    data:data,
			    success:function (req){
			      if(req.status==200){
			      	if(type==1){
			      		$('#db_key_'+data.id).remove();
			      	}else if(type==2){
			      		$('#file_key_'+data.id).remove();
			      	}
			      }else{
			        show_msg({msg:req.msg,type:'error',time:2000});
			      }
			      return false;
			    },
			    error:function (){
			      show_msg({msg:'系统异常',type:'error',time:2000});
			    }
		  });
		})
		.on('click','._delete_cancle',function (){
			//删除取消
			$('._delete_id').val('');
			$('._delete_type').val('');
		})

		;
		
})


//获取数据列表
function getlist(option){
	var default_option = {
	    page : 1,//当前页码
	    pagesize : pagesize,
	    type:1//tab类型
	  }
	option = $.extend({},default_option,option);
	$.ajax({
	    url:'/Structure/ajaxGetListData.html',
	    type:'POST',
	    dataType:'json',
	    async:true,
	    data:option,
	    success:function (req){
	      if(req.status==200){
//	      	console.log(req.data);
	        //渲染数据
	        var html = template(tempalte_div_box, req.data);

	        document.getElementById(target_div_box).innerHTML = html;

	        // //获取分页html代码
	        // var page = _37degree_setpage(req.data.page,req.data.totalpage);//当前页码，总页数
	        // $('._page_info').html(page);

	      }else{
	        show_msg({msg:req.msg,type:'error',time:2000});
	      }
	      return false;
	    },
	    error:function (){
	      show_msg({msg:'系统异常',type:'error',time:2000});
	    }
  });

}

//切换分页时
function gotopage(current_page){
  //获取分页数据
  getlist({page:current_page,type:type,pagesize:pagesize});
}